package com.qdxwx.web.controller.picture;

import com.qdxwx.common.page.PageRequest;
import com.qdxwx.common.utils.TKConvert;
import com.qdxwx.common.utils.TKRequest;
import com.qdxwx.data.service.channel.ChannelService;
import com.qdxwx.data.service.picture.PictureService;
import com.qdxwx.models.channel.Channel;
import com.qdxwx.models.enums.ModuleType;
import com.qdxwx.models.picture.Picture;
import com.qdxwx.models.picture.PictureQuery;
import com.qdxwx.web.controller.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import jakarta.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/picture")
public class PictureIndexController extends BaseController {

    @Autowired
    private ChannelService channelService;

    @Autowired
    private PictureService pictureService;

    @RequestMapping("/demo.html")
    public String showCourseIndex(HttpServletRequest request, ModelMap model) {
        if (TKRequest.isMobileBrowse(request)){
            return getViewPathH5("picture/picture-list");
        }
        int channelId = TKConvert.toInteger(request.getParameter("channelId"));
        if (channelId == 0) {
            channelId = 10;
        }
        Channel channel = channelService.get(channelId);
        if (channel == null) {
            return toError404(request);
        }
        //禁用的频道
        if (channel.getIsDisabled() == 1) {
            return toError404(request);
        }
        //判断是否属于模块
        if (channel.getModuleId() != ModuleType.PICTURE.ordinal()) {
            return toError404(request);
        }
        PageRequest pageRequest = new PageRequest(1,8);
        PictureQuery params = new PictureQuery();
        params.setStatus(1);
        List<Picture> pictureNews = pictureService.getList(params, pageRequest);
        params.setIsElite(1);
        List<Picture> pictureElites = pictureService.getList(params, pageRequest);
        params.setIsElite(null);
        params.setOrderBy(1);
        List<Picture> pictureviews = pictureService.getList(params, pageRequest);
        model.put("channel", channel);
        model.put("pictureList", pictureNews);
        model.put("pictureList", pictureElites);
        model.put("pictureList", pictureviews);
        return getViewPath("picture/picture-index");
    }
}
